package com.ada.lbs.v3.service;

import java.io.IOException;

import com.ada.http.Connection;
import com.ada.http.Connection.Method;
import com.ada.http.HttpConnection;
import com.ada.lbs.v3.domain.BoundsSearch;
import com.ada.lbs.v3.domain.LbsSearch;
import com.ada.lbs.v3.domain.LocalSearch;
import com.ada.lbs.v3.domain.Search;
import com.google.gson.Gson;

public class LbsSearchService {

	private String nearbyurl = "http://api.map.baidu.com/geosearch/v3/nearby";
	private String localurl = "http://api.map.baidu.com/geosearch/v3/local";
	private String boundurl = "http://api.map.baidu.com/geosearch/v3/bound";


	public LbsSearch serachForNearby(Search search) {
		LbsSearch result = null;
		Connection connection = HttpConnection.connect(nearbyurl);
		connection.data("ak", Config.AK);
		connection.data("geotable_id", Config.geotable_id);
		connection.data("q", "" + search.getQ());
		connection.data("location", "" + search.getLocation());
		connection.data("coord_type", "" + search.getCoord_type());
		connection.data("radius", "" + search.getRadius());
		if (search.getTags() != null) {
			connection.data("tags", "" + search.getTags());

		}
		if (search.getSortby() != null) {
			connection.data("sortby", "" + search.getSortby());

		}
		if (search.getFilter() != null) {
			connection.data("filter", "" + search.getFilter());

		}
		connection.data("page_index", "" + search.getPage_index());
		connection.data("page_size", "" + search.getPage_size());
		if (search.getCallback() != null) {
			connection.data("callback", "" + search.getCallback());

		}
		if (search.getSn() != null) {
			connection.data("sn", "" + search.getSn());
		}
		// connection.data("sn", ""+poi.getCoord_type());
		connection.method(Method.GET);
		String msg = null;
		try {
			msg = connection.execute().body();
			Gson gson = new Gson();
			result = gson.fromJson(msg, LbsSearch.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (msg == null) {
		}
		return result;
	}

	public LbsSearch findBylocal(LocalSearch search) {
		LbsSearch result = null;
		Connection connection = HttpConnection.connect(localurl);
		connection.data("ak", Config.AK);
		connection.data("geotable_id", Config.geotable_id);
		connection.data("q", "" + search.getQ());
		connection.data("coord_type", "" + search.getCoord_type());
		if (search.getRegion() != null) {
			connection.data("region", "" + search.getRegion());
		}
		if (search.getTags() != null) {
			connection.data("tags", "" + search.getTags());

		}
		if (search.getSortby() != null) {
			connection.data("sortby", "" + search.getSortby());

		}
		if (search.getFilter() != null) {
			connection.data("filter", "" + search.getFilter());

		}
		connection.data("page_index", "" + search.getPage_index());
		connection.data("page_size", "" + search.getPage_size());
		if (search.getCallback() != null) {
			connection.data("callback", "" + search.getCallback());

		}
		if (search.getSn() != null) {
			connection.data("sn", "" + search.getSn());
		}
		// connection.data("sn", ""+poi.getCoord_type());
		connection.method(Method.GET);
		String msg = null;
		try {
			msg = connection.execute().body();
			Gson gson = new Gson();
			result = gson.fromJson(msg, LbsSearch.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (msg == null) {
		}
		return result;
	}

	public LbsSearch findBybound(BoundsSearch search) {
		LbsSearch result = null;
		Connection connection = HttpConnection.connect(boundurl);
		connection.data("ak", Config.AK);
		connection.data("geotable_id", Config.geotable_id);
		connection.data("q", "" + search.getQ());
		connection.data("coord_type", "" + search.getCoord_type());
		connection.data("bounds", "" + search.getBounds());
		if (search.getTags() != null) {
			connection.data("tags", "" + search.getTags());

		}
		if (search.getSortby() != null) {
			connection.data("sortby", "" + search.getSortby());

		}
		if (search.getFilter() != null) {
			connection.data("filter", "" + search.getFilter());

		}
		connection.data("page_index", "" + search.getPage_index());
		connection.data("page_size", "" + search.getPage_size());
		if (search.getCallback() != null) {
			connection.data("callback", "" + search.getCallback());

		}
		if (search.getSn() != null) {
			connection.data("sn", "" + search.getSn());
		}
		// connection.data("sn", ""+poi.getCoord_type());
		connection.method(Method.GET);
		String msg = null;
		try {
			msg = connection.execute().body();
			Gson gson = new Gson();
			result = gson.fromJson(msg, LbsSearch.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (msg == null) {
		}
		return result;
	}
}
